<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="rowDel">
                <template slot-scope="scope" slot="menu">
                    <el-button type="text" size="small">下载合同</el-button>
                </template>
                <template slot="menuLeft">
                    <div>
                        <el-button type="primary" size="small" @click="dialogFormVisible = true">新增合同</el-button>
                        <el-dialog title="新增合同" :visible.sync="dialogFormVisible">
                            <el-form :model="form">
                                <el-form-item label="企业名称" label-width="30%" required>
                                    <el-input v-model="form.name" autocomplete="off" style="width: 70%"></el-input>
                                </el-form-item>
                                <el-form-item label="合同编号" label-width="30%" required>
                                    <el-input v-model="form.id" autocomplete="off" style="width: 70%"></el-input>
                                </el-form-item>
                                <el-form-item label="合同名称" label-width="30%" required>
                                    <el-input v-model="form.pactName" autocomplete="off" style="width: 70%"></el-input>
                                </el-form-item>
                                <el-form-item label="合同签署时间" label-width="30%" required>
                                    <div style="width: 70%;display: inline-block">
                                        <el-date-picker
                                                v-model="form.time"
                                                type="date"
                                                placeholder="选择日期">
                                        </el-date-picker>
                                    </div>
                                </el-form-item>
                                <el-form-item label="上传合同" label-width="30%" required>
                                    <el-upload
                                            class="upload-demo"
                                            drag
                                            action="https://jsonplaceholder.typicode.com/posts/"
                                            multiple>
                                        <i class="el-icon-upload"></i>
                                        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                                    </el-upload>
                                </el-form-item>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="dialogFormVisible = false">保存</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </template>
            </avue-crud>
        </basic-container>
    </div>
</template>

<script>
    import {
        fetchList,
        getObj,
        addObj,
        putObj,
        delObj
    } from '@/api/purchase/purchaseorder'
    import {
        tableOption
    } from '@/const/crud/scm/pact/purchasepact'
    import {
        mapGetters
    } from 'vuex'

    export default {
        name: 'suppliergrade',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                form:{
                    name:'',
                    id:'',
                    pactName:'',
                    time:''
                },
                dialogFormVisible: false,
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.purchase_purchasetask_add, true),
                    delBtn: this.vaildData(this.permissions.purchase_purchasetask_del, true),
                    editBtn: this.vaildData(this.permissions.purchase_purchasetask_edit, true)
                };
            }
        },
        methods: {
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            rowDel: function (row, index) {
                var _this = this
                this.$confirm('是否确认删除ID为' + row.id, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.id)
                }).then(data => {
                    _this.tableData.splice(index, 1)
                    _this.$message({
                        showClose: true,
                        message: '删除成功',
                        type: 'success'
                    })
                    this.getList(this.page)
                }).catch(function (err) {
                })
            },
            /**
             * @title 数据更新
             * @param row 为当前的数据
             * @param index 为当前更新数据的行数
             * @param done 为表单关闭函数
             *
             **/
            handleUpdate: function (row, index, done) {
                putObj(row).then(data => {
                    this.tableData.splice(index, 1, Object.assign({}, row))
                    this.$message({
                        showClose: true,
                        message: '修改成功',
                        type: 'success'
                    })
                    done()
                    this.getList(this.page)
                })
            },
            /**
             * @title 数据添加
             * @param row 为当前的数据
             * @param done 为表单关闭函数
             *
             **/
            handleSave: function (row, done) {
                addObj(row).then(data => {
                    this.tableData.push(Object.assign({}, row))
                    this.$message({
                        showClose: true,
                        message: '添加成功',
                        type: 'success'
                    })
                    done()
                    this.getList(this.page)
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
}
        }
    }
</script>

<style lang="scss" scoped>
</style>
